package groupby.action;

import groupby.manage.GroupCriticismManage;
import groupby.manage.GroupbyManage;
import groupby.model.Category;
import groupby.model.GroupCriticism;
import groupby.model.GroupUserCriticism;
import groupby.model.Groupby;
import groupby.util.Pages;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import user.manage.UsersManage;
import user.model.Dynamic;
import user.model.UserDetial;
import user.model.Users;

import com.opensymphony.xwork2.ActionSupport;

import dic.manage.DicManage;
import dic.model.City;

public class GroupCriticismAction extends ActionSupport {

	private GroupbyManage groupbyManage = null;
	private GroupCriticismManage groupCriticismManage = null;
	private UsersManage userManage = null;
	private DicManage dicManage = null;
	private int id;
	private int groupbyId;
	private int groupbyUserId;
	private int userId;
	private int cityId;
	private UserDetial userDetial=null;
	private String content = null;
	private Groupby groupby = null;
	private int currentPage;
	private List groupbyCriticismList = null;
	
	private String tags = null;
	private Pages pages = null;
	private String path = null;
	private String categoryId = null;
	private String paramStr = null;
    private List listDredgeCitys=null;
    private String url=null;

	@Override
	public String execute() throws Exception {
		return "success";
	}
    /*
     * 
     * 评论团购活动
     */
	public String addGroupCriticism() throws Exception {
		GroupCriticism groupCriticism=new GroupCriticism();
		Users users = (Users) ServletActionContext.getRequest().getSession().getAttribute("user");
		if(users==null)
		{
			 this.addActionError("你没有登录，请登录！");
			return "input";
		}else
		{
			groupCriticism.setUserId(users.getId());
			groupCriticism.setGroupbyId(groupbyId);
			groupCriticism.setContent(content);
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			groupCriticism.setDatetime(sdf.format(new Date()));
			groupCriticismManage.addGroupCriticism(groupCriticism);
			
			
			Dynamic  dynamic=new Dynamic();
			UserDetial userDetial=userManage.getSingleUser(users.getId());
			Groupby groupby = groupbyManage.criticismGroupbyId(groupbyId);
			if(userDetial!=null&&!userDetial.getUsername().equals(""))
			{
			   dynamic.setUser1(userDetial.getUsername()+"<>"+users.getId());
			  
			}else
			{
				dynamic.setUser1(users.getEmail()+"<>"+users.getId());
			}
			
			dynamic.setUser2(groupby.getWebsite()+"<>"+groupbyId+","+currentPage+","+cityId);
			
			dynamic.setContent(content);
			dynamic.setFlag(5);
			
			dynamic.setDatetime(sdf.format(new Date()));
			userManage.addDynamic(dynamic);
		
			City city=dicManage.findCityById(cityId);
	    	 paramStr="cityId="+cityId+"&";
	    	 if(categoryId!=null)
		      {
		    	  Category category=groupbyManage.findCategoryById(Integer.parseInt(categoryId));
			      paramStr="categoryId="+categoryId+"&";
		      }
		}
		return "AddGroupCriticism";
	}
	 /*
     * 
     * 评论团购用户
     */
	public String addGroupUserCriticism() throws Exception {
		GroupUserCriticism groupCriticism=new GroupUserCriticism();
		Users users = (Users) ServletActionContext.getRequest().getSession().getAttribute("user");
		
			groupCriticism.setUserId(users.getId());
			groupCriticism.setGroupbyUserId(userId);
			groupCriticism.setContent(content);
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			groupCriticism.setDatetime(sdf.format(new Date()));
			groupCriticism.setUrl(url);
			groupCriticismManage.addGroupUserCriticism(groupCriticism);
			
			
			Dynamic  dynamic=new Dynamic();
			UserDetial userDetial=userManage.getSingleUser(users.getId());
			
			if(userDetial!=null&&!userDetial.getUsername().equals(""))
			{
			   dynamic.setUser1(userDetial.getUsername()+"<>"+users.getId());
			  
			}else
			{
				dynamic.setUser1(users.getEmail()+"<>"+users.getId());
			}
			userDetial = userManage.getSingleUser(userId);
			dynamic.setUser2(userDetial.getUsername()+"<>"+userId);
			dynamic.setDatetime(sdf.format(new Date()));
			dynamic.setContent(content);
			dynamic.setFlag(7);
			userManage.addDynamic(dynamic);
		
		return "AddGroupUserCriticism";
	}
	// 去评论团购活动
	public String criticismGroupby() {
		Users users = (Users) ServletActionContext.getRequest().getSession()
				.getAttribute("user");
		
			groupby = groupbyManage.criticismGroupbyId(groupbyId);
			City city = dicManage.findCityById(cityId);
			path = "<a style='color:#9C2684;font-family:微软雅黑;font-size:14px;font-weight:bold;' href='groupby!index.action?currentPage=1&cityId="+cityId+"'>" + city.getName() + "</a>";
			paramStr = "cityId=" + cityId + "&";
			if (categoryId != null) {
				Category category = groupbyManage.findCategoryById(Integer
						.parseInt(categoryId));
				path += " > <a style='color:#9C2684;font-family:微软雅黑;font-size:14px;font-weight:bold;' href='groupby!index.action?currentPage=1&cityId="+cityId+"'>"
						+ category.getCategoryName() + "</a>";
				paramStr = "categoryId=" + categoryId + "&";
			}
			String path = ServletActionContext.getRequest().getRealPath(
			"/WEB-INF/classes/tags.xml");
			tags = groupbyManage.getTags(path);
			pages = new Pages();
			pages.setCurrentPage(currentPage);
			pages.setPageSize(6);
			groupbyCriticismList = groupCriticismManage.getGroupbyCriticismList(groupbyId, pages);
			listDredgeCitys=dicManage.getDredgeCitys();
		
		return "success";
	}
	// 去评论团购用户
	public String criticismGroupbyUser() {
		
		
			 userDetial = userManage.getSingleUser(userId);
			
			pages = new Pages();
			pages.setCurrentPage(currentPage);
			pages.setPageSize(6);
			groupbyCriticismList = groupCriticismManage.getGroupbyUserCriticismList(userId, pages);

		return "criticismGroupbyUser";
	}
	
	
	public GroupCriticismManage getGroupCriticismManage() {
		return groupCriticismManage;
	}

	public void setGroupCriticismManage(
			GroupCriticismManage groupCriticismManage) {
		this.groupCriticismManage = groupCriticismManage;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getGroupbyId() {
		return groupbyId;
	}

	public void setGroupbyId(int groupbyId) {
		this.groupbyId = groupbyId;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Groupby getGroupby() {
		return groupby;
	}

	public GroupbyManage getGroupbyManage() {
		return groupbyManage;
	}

	public void setGroupbyManage(GroupbyManage groupbyManage) {
		this.groupbyManage = groupbyManage;
	}

	public List getGroupbyCriticismList() {
		return groupbyCriticismList;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public String getTags() {
		return tags;
	}

	public Pages getPages() {
		return pages;
	}

	public int getCityId() {
		return cityId;
	}

	public void setCityId(int cityId) {
		this.cityId = cityId;
	}

	public DicManage getDicManage() {
		return dicManage;
	}

	public void setDicManage(DicManage dicManage) {
		this.dicManage = dicManage;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public String getParamStr() {
		return paramStr;
	}

	public UsersManage getUserManage() {
		return userManage;
	}

	public void setUserManage(UsersManage userManage) {
		this.userManage = userManage;
	}

	public int getGroupbyUserId() {
		return groupbyUserId;
	}

	public void setGroupbyUserId(int groupbyUserId) {
		this.groupbyUserId = groupbyUserId;
	}

	public UserDetial getUserDetial() {
		return userDetial;
	}
	public List getListDredgeCitys() {
		return listDredgeCitys;
	}
	public int getUserId() {
		return userId;
	}
	public void setUserId(int userId) {
		this.userId = userId;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	
}
